package com.jyw.business.obstacles.domain.vo.web;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson2.JSONObject;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.jyw.business.obstacles.constant.OrderProcessStateConstant;
import com.jyw.common.constant.SysDictTypeConstant;
import com.jyw.business.obstacles.domain.ObsOrderProcess;
import com.jyw.business.obstacles.enums.OrderFieldsEnum;
import com.jyw.common.annotation.Conversion;
import com.jyw.common.annotation.Excel;
import com.jyw.common.enums.ConversionType;
import com.jyw.common.utils.DictUtils;
import com.jyw.common.utils.bean.BeanUtils;
import com.jyw.system.utils.DeptUtils;
import com.jyw.system.utils.FileUtils;
import com.jyw.system.utils.TeamUtils;
import com.jyw.system.utils.UserUtils;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.FieldNameConstants;
import org.springframework.util.ObjectUtils;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Map;

/**
 * @author 沈峻
 * @ClassName ProcessVO
 * @Description 流程信息
 * @Date 2023/8/21 10:21
 **/
@Data
@ApiModel("流程信息")
@NoArgsConstructor
@FieldNameConstants
public class ProcessVO {

    private static final String NEW_VALUE = "newValue";

    private static final String OLD_VALUE = "oldValue";

    @ApiModelProperty("工单号")
    private String orderNum;

    /** 备注 */
    @ApiModelProperty("备注")
    private String remark;

    /** 上次工单状态（数据字典） */
    @ApiModelProperty("上次工单状态（数据字典）")
    private String lastFaultState;

    /** 操作类型（数据字典） */
    @ApiModelProperty("操作类型（数据字典）")
    @Conversion(value = ConversionType.DICT,dictType = SysDictTypeConstant.OBS_PROCESS_TYPE)
    private String operState;

    @ApiModelProperty("操作类型标签")
    private String operStateToName;

    /** 派单对象 */
    @ApiModelProperty("派单对象")
    private String send;

    /** 参数 */
    @ApiModelProperty("参数")
    private String processValue;

    /** 附件 */
    @ApiModelProperty("附件")
    private String processEnclosure;

    /** 处理状态 */
    @ApiModelProperty("处理状态")
    @Conversion(value = ConversionType.DICT,dictType = SysDictTypeConstant.OBS_MAKE_TYPE)
    private String makeState;

    @ApiModelProperty("处理状态标签")
    private String makeStateToName;

    /** 创建人(创建人) */
    @ApiModelProperty("创建人(创建人)")
    @Conversion(value = ConversionType.USER,name = Fields.createUserName)
    private Long createUser;

    @ApiModelProperty("创建人名称")
    private String createUserName;


    /** 创建时间(创建时间) */
    @ApiModelProperty("创建时间(创建时间)")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm")
    private Date createTime;


    /** 驳回原因 */
    @ApiModelProperty("驳回原因")
    @Excel(name = "驳回原因")
    private String rejectReason;

    /** 驳回描述 */
    @ApiModelProperty("驳回说明")
    @Excel(name = "驳回说明")
    private String rejectDescribe;

    public ProcessVO(ObsOrderProcess process) {
        BeanUtils.copyProperties(process,this);
        if(process.getOperState().equals(OrderProcessStateConstant.CREATE_ORDER)){
            //创建工单
            setProcessValue(DictUtils.getDictLabel(SysDictTypeConstant.OBS_FAULT_SOURCE,getProcessValue()));
        } else if (process.getOperState().equals(OrderProcessStateConstant.RECEIVING)) {
            // 接单类型
            setProcessValue(DictUtils.getDictLabel(SysDictTypeConstant.OBS_RECEIVING_TYPE,getProcessValue()));
        } else if (process.getOperState().equals(OrderProcessStateConstant.ALLOCATE)) {
            //派单
            allocate(process);
        } else if (process.getOperState().equals(OrderProcessStateConstant.HURRY)) {
            //催单 暂不需要处理
        } else if (process.getOperState().equals(OrderProcessStateConstant.EDIT_ORDER)) {
            //修改工单
            setProcessValue(castEditField());
        } else if (process.getOperState().equals(OrderProcessStateConstant.MAKING)) {
            //处理工单 暂不处理
        } else if (process.getOperState().equals(OrderProcessStateConstant.EXAMINE)) {

        }else if (process.getOperState().equals(OrderProcessStateConstant.EVALUATE)) {
            JSONObject jsonObject = JSONObject.parseObject(getProcessValue());
            if(jsonObject!=null&&jsonObject.getString("evaluateResult")!=null){
                setProcessValue(DictUtils.getDictLabel(SysDictTypeConstant.OBS_EVALUATE_RESULT,jsonObject.getString("evaluateResult")));
            }
        }else if (process.getOperState().equals(OrderProcessStateConstant.REPEAT)) {

        }
    }
    //派单数据封装
    private void allocate(ObsOrderProcess process) {
        if(!ObjectUtils.isEmpty(process.getSendUser())){
            setSend(UserUtils.userIdToName(process.getSendUser()));
        } else if (!ObjectUtils.isEmpty(process.getSendTeam())) {
            setSend(TeamUtils.teamIdToName(process.getSendTeam()));
        } else if (!ObjectUtils.isEmpty(process.getSendDept())) {
            setSend(DeptUtils.deptIdToName(process.getSendDept()));
        }
    }

    /**
     * 编辑工单数据转换
     * @param
     * @return
     */
    private String castEditField(){
        //如果是编辑工单那么数据进行转换
        JSONObject jsonObject = JSONObject.parseObject(getProcessValue());
        JSONObject result = new JSONObject();
        Map<String, String> fieldMap = OrderFieldsEnum.getMap();
        for (String key : jsonObject.keySet()) {
            //获取每个字段变更的数据
            JSONObject jsonFields = jsonObject.getJSONObject(key);
            if(OrderFieldsEnum.SOURCE.getCode().equals(key)){
                //工单来源
                jsonFields.put(NEW_VALUE,DictUtils.getDictLabel(SysDictTypeConstant.OBS_FAULT_SOURCE,jsonFields.getString(NEW_VALUE)));
                jsonFields.put(OLD_VALUE,DictUtils.getDictLabel(SysDictTypeConstant.OBS_FAULT_SOURCE,jsonFields.getString(OLD_VALUE)));
            }else if(OrderFieldsEnum.ACTUAL_REPAIR_PLACE.getCode().equals(key)){
                //实际报修地址
                jsonFields.put(NEW_VALUE,DictUtils.getDictLabel(SysDictTypeConstant.OBS_REPAIR_PLACE,jsonFields.getString(NEW_VALUE)));
                jsonFields.put(OLD_VALUE,DictUtils.getDictLabel(SysDictTypeConstant.OBS_REPAIR_PLACE,jsonFields.getString(OLD_VALUE)));
            } else if (OrderFieldsEnum.DEGREE.getCode().equals(key)) {
                //紧急程度
                jsonFields.put(NEW_VALUE,DictUtils.getDictLabel(SysDictTypeConstant.OBS_FAULT_DEGREE,jsonFields.getString(NEW_VALUE)));
                jsonFields.put(OLD_VALUE,DictUtils.getDictLabel(SysDictTypeConstant.OBS_FAULT_DEGREE,jsonFields.getString(OLD_VALUE)));
            } else if (OrderFieldsEnum.RANGE_TYPE.getCode().equals(key)) {
                //影响范围
                jsonFields.put(NEW_VALUE,DictUtils.getDictLabel(SysDictTypeConstant.OBS_FAULT_RANGE,jsonFields.getString(NEW_VALUE)));
                jsonFields.put(OLD_VALUE,DictUtils.getDictLabel(SysDictTypeConstant.OBS_FAULT_RANGE,jsonFields.getString(OLD_VALUE)));
            }else if (OrderFieldsEnum.RESPONSE_TYPE.getCode().equals(key)) {
                //响应类型
                jsonFields.put(NEW_VALUE,DictUtils.getDictLabel(SysDictTypeConstant.OBS_FAULT_RESPONSE,jsonFields.getString(NEW_VALUE)));
                jsonFields.put(OLD_VALUE,DictUtils.getDictLabel(SysDictTypeConstant.OBS_FAULT_RESPONSE,jsonFields.getString(OLD_VALUE)));
            }else if (OrderFieldsEnum.RESPONSE_TYPE.getCode().equals(key)) {
                //响应类型
                jsonFields.put(NEW_VALUE,DictUtils.getDictLabel(SysDictTypeConstant.OBS_FAULT_RESPONSE,jsonFields.getString(NEW_VALUE)));
                jsonFields.put(OLD_VALUE,DictUtils.getDictLabel(SysDictTypeConstant.OBS_FAULT_RESPONSE,jsonFields.getString(OLD_VALUE)));
            }else if (OrderFieldsEnum.FAULT_TYPE.getCode().equals(key)) {
                //故障类型
                jsonFields.put(NEW_VALUE,DictUtils.getDictLabel(SysDictTypeConstant.OBS_FAULT_TYPE,jsonFields.getString(NEW_VALUE)));
                jsonFields.put(OLD_VALUE,DictUtils.getDictLabel(SysDictTypeConstant.OBS_FAULT_TYPE,jsonFields.getString(OLD_VALUE)));
            }else if (OrderFieldsEnum.DEPT_ID.getCode().equals(key)) {
                //报修科室
                jsonFields.put(NEW_VALUE, DeptUtils.deptIdToName(jsonFields.getLong(NEW_VALUE)));
                jsonFields.put(OLD_VALUE,DeptUtils.deptIdToName(jsonFields.getLong(OLD_VALUE)));
            }else if (OrderFieldsEnum.DEPT_ID.getCode().equals(key)) {
                //报修科室
                jsonFields.put(NEW_VALUE, DeptUtils.deptIdToName(jsonFields.getLong(NEW_VALUE)));
                jsonFields.put(OLD_VALUE,DeptUtils.deptIdToName(jsonFields.getLong(OLD_VALUE)));
            }else if (OrderFieldsEnum.NEED_TIME.getCode().equals(key)) {
                //所需工时
                if (!ObjectUtils.isEmpty(jsonFields.get(NEW_VALUE))) {
                    jsonFields.put(NEW_VALUE,jsonFields.getBigDecimal(NEW_VALUE).divide(BigDecimal.valueOf(60)));
                }
                if (!ObjectUtils.isEmpty(jsonFields.get(OLD_VALUE))) {
                    jsonFields.put(OLD_VALUE,jsonFields.getBigDecimal(OLD_VALUE).divide(BigDecimal.valueOf(60)));
                }
            }else if (OrderFieldsEnum.COMPLETE_TIME.getCode().equals(key)||OrderFieldsEnum.APPOINTMENT.getCode().equals(key)) {
                //完成时间和预约时间
                if (!ObjectUtils.isEmpty(jsonFields.get(NEW_VALUE))) {
                    jsonFields.put(NEW_VALUE, DateUtil.formatDateTime(jsonFields.getDate(NEW_VALUE)));
                }
                if (!ObjectUtils.isEmpty(jsonFields.get(OLD_VALUE))) {
                    jsonFields.put(OLD_VALUE, DateUtil.formatDateTime(jsonFields.getDate(OLD_VALUE)));
                }
            } else if (OrderFieldsEnum.ENCLOSURE.getCode().equals(key)) {
                jsonFields.put(NEW_VALUE, FileUtils.IdToFile(jsonFields.get(NEW_VALUE).toString()));
                jsonFields.put(OLD_VALUE,FileUtils.IdToFile(jsonFields.get(OLD_VALUE).toString()));
            }
            result.put(fieldMap.get(key),jsonFields);
        }
        return result.toJSONString();
    }
}
